<template>
	<view class="category-manage">
		<scroll-view class="category-manage-view" scroll-y>
			<view class="category-manage-list" v-if="categoryList.length">
				<view class="category-item" v-for="(data,index) in categoryList" :key="index" @click="goProductManage(data.class_id)">
					<view class="category-item-name">{{data.class_name}}</view>
					<view class="category-item-right">
						<view class="category-item-number" v-if="!manage">{{data.goods_count}}件商品<image src="https://master.shengejiaju.cn/web/static/icons/ico_more_goods@2x.png"></image></view>
						<view class="category-item-action" v-else>
							<view class="edit" @click.stop="edit(data.class_id,data.class_name,data.img)">编辑</view>
							<view class="del" @click.stop="del(data.class_id)">删除</view>
							<view class="up" @click.stop="up(data.class_id)" v-if="data.status==2">上架</view>
							<view class="down" @click.stop="down(data.class_id)" v-else>下架</view>
						</view>
					</view>
				</view>
			</view>
			<nothing :text="'还未添加分类'" :btnTxt="'去添加'" @goUrl="edit" v-else></nothing>
		</scroll-view>
		<view class="category-manage-bottom">
			<view v-if="!manage" @click="edit()"><image src="https://master.shengejiaju.cn/web/static/icons/ico_add@2x.png"></image>新建分类</view>
			<view v-if="!manage" @click="setCategoryName()"><image src="https://master.shengejiaju.cn/web/static/icons/setting.png"></image>分类名称</view>
			<view v-if="!manage" @click="manageFun"><image src="https://master.shengejiaju.cn/web/static/icons/ico_edit@2x.png"></image>管理分类</view>
			<view class="complete" @click="complete" v-else>完成</view>
		</view>
		<modal :title="title" :value="value" :icon="img" :placeholderTxt="placeholderTxt" @confirm="confirm" @cancel="cancel" v-if="show" :show="show"></modal>
		<uni-popup ref="categoryName" type="dialog">
			<uni-popup-dialog mode="input" placeholder="请设置2个字符的中文分类名称"
				:value="categoryName"
				@confirm="confirmCategoryName"
				:title="categoryName?'请修改分类名称':'请设置分类名称'">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isRefresh: false,
				title: '新建分类',
				placeholderTxt: '输入分类名称',
				value: '',
				class_id: '',
				img: '',
				show: false,
				manage: false,
				categoryList: [],
				is_package: false,
				categoryName: ''
			};
		},
		onLoad(options) {
			if(options.is_package){
				this.is_package = true
			}
		},
		onShow() {
			this.init()
		},
		methods:{
			init(){
				let data = {
					status: 1,
					limit: 100,
					type: 1,
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id
				}
				this.$api.StoreGoodsClassList(data).then(res => {
					if(res.class_name){
						this.categoryName = res.class_name
						uni.setNavigationBarTitle({
							title: res.class_name
						})
					}
					this.categoryList = res.datas
				})
			},
			cancel(){
				this.show = false
			},
			confirm(e){
				if(this.class_id){
					let data = {
						class_id: this.class_id,
						class_name: JSON.parse(e).text,
						img:  JSON.parse(e).img?JSON.parse(e).img:'',
						store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
						status: 1
					}
					this.$api.UpGoodsClass(data).then(res => {
						uni.showToast({
							icon: 'none',
							title: '修改成功',
							duration: 1500
						})
					})
				}else{
					let data = {
						class_name: JSON.parse(e).text,
						img:  JSON.parse(e).img,
						store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
						status: 1
					}
					this.$api.AddGoodsClass(data).then(res => {
						uni.showToast({
							icon: 'none',
							title: '添加成功',
							duration: 1500
						})
					})
				}
				setTimeout(() => {
					this.show = false
					this.init()
				},1500)
			},
			edit(id,name,img){
				this.show = true
				if(id){
					this.title = '编辑分类'
					this.value = name
					this.class_id = id
					this.img = img
				}else{
					this.title = '新建分类'
					this.value = ''
					this.class_id = ''
				}
			},
			del(id){
				let _this = this
				uni.showModal({
					content: '是否删除此分类？',
					success(res) {
						if(res.confirm){
							_this.$api.DelGoodsClass({class_id: id}).then(res => {
								uni.showToast({
									icon: 'none',
									title: '删除成功',
									duration: 1500
								})
								setTimeout(() => {
									_this.init()
								},1500)
							})
						}
					}
				})
			},
			up(id){
				let _this = this
				uni.showModal({
					content: '是否上架此分类？',
					success(res) {
						if(res.confirm){
							_this.$api.UpGoodsClass({class_id: id,status: 1}).then(res => {
								uni.showToast({
									icon: 'none',
									title: '已上架',
									duration: 1500
								})
								setTimeout(() => {
									_this.init()
								},1500)
							})
						}
					}
				})
			},
			down(id){
				let _this = this
				uni.showModal({
					content: '是否下架此分类？',
					success(res) {
						if(res.confirm){
							_this.$api.UpGoodsClass({class_id: id,status: 2}).then(res => {
								uni.showToast({
									icon: 'none',
									title: '已下架',
									duration: 1500
								})
								setTimeout(() => {
									_this.init()
								},1500)
							})
						}
					}
				})
			},
			manageFun(){
				this.manage = true
			},
			complete(){
				this.manage = false
			},
			goProductManage(id){
				if(this.is_package){
					uni.navigateTo({
						url: '/pages/product-manage/product-manage?type='+id +'&is_package=1'
					})
				}else{
					uni.navigateTo({
						url: '/pages/product-manage/product-manage?type='+id
					})
				}
			},
			setCategoryName(){
				this.$refs.categoryName.open()
			},
			confirmCategoryName(done,value){
				let data = {
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
					class_name: value
				}
				if(!value){
					uni.showToast({
						icon: 'none',
						title: '分类名称不能为空'
					})
					return false
				}else if(value && value.length > 2){
					uni.showToast({
						icon: 'none',
						title: '分类名称不能大于2个字符'
					})
					return false
				}
				this.$api.AddStoreCategory(data).then(res => {
					this.categoryName = value
					this.$refs.categoryName.close()
					uni.setNavigationBarTitle({
						title: value
					})
					uni.showToast({
						icon: 'none',
						title: !this.categoryName?'设置成功':'修改成功'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	@import  '../../static/scss/category-manage.scss';
	.category-manage-view{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 98rpx;
	}
</style>